namespace System.Linq.Reactive
{
    public class LamdaObservable<T> : IObservableEx<T>
    {
        private readonly IObserverEx<T> f_Observer;
        private readonly Action<IObserverEx<T>, T> f_OnNext;
        private readonly Action<IObserverEx<T>> f_OnComplited;
        private readonly Action<IObserverEx<T>> f_OnReset;
        private readonly Action<IObserverEx<T>, Exception> f_OnError;

        public LamdaObservable(IObserverEx<T> observer, Action<IObserverEx<T>, T> OnNext,
                    Action<IObserverEx<T>> OnComplited = null,
                    Action<IObserverEx<T>> OnReset = null,
                    Action<IObserverEx<T>, Exception> OnError = null)
        {
            f_Observer = observer;
            f_OnNext = OnNext;
            f_OnComplited = OnComplited;
            f_OnReset = OnReset;
            f_OnError = OnError;
        }

        public IObserverEx<T> GetObserver()
        {
            var observer = new SimpleObserverEx<T>();
            f_Observer.Next += t => f_OnNext(observer, t);
            if(f_OnComplited != null) f_Observer.Complited += () => f_OnComplited(observer); else f_Observer.Complited += observer.OnComplited;
            if(f_OnComplited != null) f_Observer.Reset += () => f_OnReset(observer); else f_Observer.Reset += observer.OnReset;
            if(f_OnComplited != null) f_Observer.Exception += error => f_OnError(observer, error); else f_Observer.Exception += observer.OnException;
            return observer;
        }
    }
}